﻿using AutoMapper;

namespace Basic.Repository.Abstract.Models;

/// <summary>
/// 通用分页信息类
/// </summary>
public class PagedModel<T>
{
    /// <summary>
    /// 当前页标
    /// </summary>
    public int Page { get; set; } = 1;

    private int? _pageCount;
    /// <summary>
    /// 总页数
    /// </summary>
    public int PageCount
    {
        get => _pageCount ??= (int)Math.Ceiling((decimal)DataCount / PageSize);
        set => _pageCount = value;
    }
    /// <summary>
    /// 数据总数
    /// </summary>
    public int DataCount { get; set; } = 0;
    /// <summary>
    /// 每页大小
    /// </summary>
    public int PageSize { set; get; } = 20;

    /// <summary>
    /// 返回数据
    /// </summary>
    public List<T> Data { get; set; }

    public PagedModel() { }

    public PagedModel(int pageIndex, int pageSize, int total, List<T> data)
    {
        this.Page = pageIndex;
        this.PageSize = pageSize;
        this.DataCount = total;
        this.Data = data;
    }

    public PagedModel<TOut> ConvertTo<TOut>()
    {
        return new PagedModel<TOut>(Page, PageSize, DataCount, default);
    }

    public PagedModel<TOut> ConvertTo<TOut>(IMapper mapper)
    {
        var model = ConvertTo<TOut>();
        if (Data != null)
        {
            model.Data = mapper.Map<List<TOut>>(Data);
        }
        return model;
    }


    public PagedModel<TOut> ConvertTo<TOut>(IMapper mapper, Action<IMappingOperationOptions> options)
    {
        var model = ConvertTo<TOut>();
        if (Data != null)
        {
            model.Data = mapper.Map<List<TOut>>(Data, options);
        }
        return model;
    }

}